/*
 * ObservationIDMapper.java is part of htn-example.
 *
 * ObservationIDMapper.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ObservationIDMapper.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ObservationIDMapper.java.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.htn.telescopes.rtml;

import org.htn.telescopes.rtml.parser.RTMLContact;
import org.htn.telescopes.rtml.parser.RTMLDocument;
import org.htn.telescopes.rtml.parser.RTMLObservation;
import org.htn.telescopes.rtml.parser.RTMLProject;
import org.htn.telescopes.rtml.parser.RTMLTarget;

public class ObservationIDMapper 
{
	/** 
	 * Gets the ObservationID from the RTMLDocument supplied.
	 * @param doc The RTMLDocument.
	 * @return A string unique to a document (observation).
	 * @exception Exception Thrown if any of the document's contact/project/obs/target are null.
	 * @see #getDBRootName
	 */
	public String createKeyFromDoc(RTMLDocument doc) throws Exception {
	
		RTMLContact contact = doc.getContact();
		if(contact == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Contact was null for document.");
		}
		String userId = contact.getUser();
	
		RTMLProject project = doc.getProject();
		if(project == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Project was null for document.");
		}
		String proposalId = project.getProject();
		// get document UId
		String requestId = doc.getUId();
		if(requestId == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Unique ID was null for document.");
		}
	
		RTMLObservation observation = doc.getObservation(0);
		if(observation == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Observation(0) was null for document.");
		}
	
		RTMLTarget target = observation.getTarget();
		if(target == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Target was null for document.");
		}
		String targetIdent = target.getIdent();
	
		return ConfigurationManager.getManager().getDBRootName()+"/"+userId+"/"+proposalId+"/"+requestId+"/"+targetIdent;
	
	} // [createKeyFromDoc(RTMLDocument doc)]
	
	/** 
	 * Gets the ObservationID from the RTMLDocument supplied.
	 * @param doc The RTMLDocument.
	 * @return A string unique to a document (observation).
	 * @exception Exception Thrown if any of the document's contact/project/obs/target are null.
	 * @see #getDBRootName
	 */
	public String createRequestIdFromDoc(RTMLDocument doc) throws Exception {
	
		RTMLContact contact = doc.getContact();
		if(contact == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Contact was null for document.");
		}
		String userId  = contact.getUser();
	
		RTMLProject project    = doc.getProject();
		if(project == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Project was null for document.");
		}
		String proposalId = project.getProject();
		// get document UId
		String requestId = doc.getUId();
		if(requestId == null)
		{
			throw new NullPointerException(this.getClass().getName()+
						       ":createKeyFromDoc:Unique ID null for document.");
		}
	
		return requestId;
	
	} // [createRequestIdFromDoc(RTMLDocument doc)]
}
